package connection;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class Connection {
	private final String host = "127.0.0.1";
	private final int port = 25;
	private IoSession session;
	private IoConnector connector;
	private static int MAX_BUFFER_SIZE = 1024 * 1024;
	
	public Connection() {
		connector = new NioSocketConnector();
		connector.getFilterChain().addLast("logger", new LoggingFilter());
		connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
		connector.setHandler(new ClientHandler());
		connector.getSessionConfig().setMaxReadBufferSize(MAX_BUFFER_SIZE);
		
		ConnectFuture future = connector.connect(new InetSocketAddress(host, port));
		future.awaitUninterruptibly();
		session = future.getSession();
	}
	
	public void send(String text)
	{
		session.write(text.replaceAll("\n", ""));
	}
}
